import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)
// 封装一个函数
function changeRouter(url,next){
  if (store.state.info.menus_url.includes(url)) {
    next()
  } else {
    next('/login')
  }
}

// 6.3导出二级路由菜单
export let IndexRoutes = [
  {
    path: 'menu',
    component: () => import('../pages/menu/menu.vue'),
    meta: {
      title: "菜单管理"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      changeRouter('/menu',next)
    }
  },
  {
    path: 'role',
    component: () => import('../pages/role/role.vue'),
    meta: {
      title: "角色管理"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      if (store.state.info.menus_url.includes("/role")) {
        next()
      } else {
        next('/login')
      }
    }
  },
  {
    path: 'manage',
    component: () => import('../pages/manage/manage.vue'),
    meta: {
      title: "管理员管理"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      changeRouter('/manage',next)
    }
  },
  {
    path: 'cate',
    component: () => import('../pages/cate/cate.vue'),
    meta: {
      title: "商品分类"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      changeRouter('/cate',next)
    }
  },
  {
    path: 'specs',
    component: () => import('../pages/specs/specs.vue'),
    meta: {
      title: "商品规格"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      changeRouter('/specs',next)
    }
  },
  {
    path: 'goods',
    component: () => import('../pages/goods/goods.vue'),
    meta: {
      title: "商品管理"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      changeRouter('/goods',next)
    }
  },
  {
    path: 'vip',
    component: () => import('../pages/vip/vip.vue'),
    meta: {
      title: "会员管理"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      changeRouter('/vip',next)
    }
  },
  {
    path: 'banner',
    component: () => import('../pages/banner/banner.vue'),
    meta: {
      title: "轮播图管理"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      changeRouter('/banner',next)
    }
  },
  {
    path: 'seckill',
    component: () => import('../pages/seckill/seckill.vue'),
    meta: {
      title: "秒杀活动"
    },
    // 路由独享守卫，看里面是否包含
    beforeEnter(to, from, next) {
      changeRouter('/seckill',next)
    }
  },

]

const routes = [
  {
    path: '/login',
    component: () => import('../pages/login/login.vue')
  },
  {
    path: '/index',
    component: () => import('../pages/index/index.vue'),
    children: [
      {
        path: 'home',
        component: () => import('../pages/home/home.vue')
      },
      //拷贝IndexrRoutes
      ...IndexRoutes,
      {
        path: '',
        redirect: 'home',

      },

    ]
  },
  {
    path: '*',
    redirect: '/login'
  },
]

const router = new VueRouter({
  routes
})

//一、登录权限
// 1.全局守卫
router.beforeEach((to, from, next) => {
  // 判断是不是登录
  if (to.path == '/login') {
    next();
    return;
  }
  //前往的不是登录，验vuex|本地存储的数据，有数据，next 
  if (!!store.state.info.token) {
    next();
    return;
  }
  //3.vuex|本地存储没有数据，next('/login')
  next('/login')
})

export default router
